package com.dgz.common.core.domain;

import com.dgz.common.core.enums.ResultCode;
import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2024/12/05/11:06
 * @Description:
 */

@Data
public class R<T> {

    private int code;
    private String msg;
    private T data;


    public static <T> R<T> ok() {
        return assembleResult(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> fail() {
        return assembleResult(null, ResultCode.FAILED);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }
    public static <T> R<T> fail(int code,String msg) {
        return assembleResult(null,code,msg);
    }



    private static <T> R<T>  assembleResult(T data, ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        r.setData(data);
        return r;
    }
    private static <T> R<T>  assembleResult(T data, int code,String msg)  {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

}
